package icraf.carbon.gui;

import icraf.carbon.core.Project;
import icraf.carbon.core.Config.ConfigItem;
import icraf.carbon.core.Zone.DataType;
import icraf.carbon.gui.LCTableModel.ValueType;

import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.event.ChangeListener;

public class LCMatrixOutputPanel extends JPanel {
	private static final long serialVersionUID = 3935329706944049274L;

	private Project project;
	private MatrixPanel netPanel = new MatrixPanel(new LCTableModel(true),
			true, true, true, true, true);
	private RowHighlight rowHighlight;
	private JTabbedPane tabPanel;
	private MatrixPanel totalNegPanel = new MatrixPanel(new LCTableModel(true),
			true, true, true, true, true);
	private MatrixPanel totalPosPanel = new MatrixPanel(new LCTableModel(true),
			true, true, true, true, true);
	private LCMatrixTabPanel zonePanel = new LCMatrixTabPanel();

	public LCMatrixOutputPanel() {
		super();
		tabPanel = new JTabbedPane();
		this.setLayout(new BorderLayout());
		this.add(tabPanel);
		rowHighlight = new RowHighlight() {
			@Override
			public Color getColor() {
				return new Color(230, 210, 250);
			}

			@Override
			public boolean isHighlight(MatrixTableModel table, int row, double f) {
				double precision = ConfigItem.PRECISION_MAX.getValueDouble();
				double precision10 = (float)Math.pow(10, precision);
				f = Math.round(f * precision10) / precision10;
				return f == 0;
			}
		};
		zonePanel.setRowHighlight(true, rowHighlight);
		netPanel.setRowHighlight(true, rowHighlight);
		totalPosPanel.setRowHighlight(true, rowHighlight);
		totalNegPanel.setRowHighlight(true, rowHighlight);
		tabPanel.addTab("1", zonePanel);
		tabPanel.addTab("2", netPanel);
		tabPanel.addTab("3", totalPosPanel);
		tabPanel.addTab("4", totalNegPanel);
		zonePanel.setEditable(false);
		LCTableModel m = (LCTableModel) netPanel.getMatrixTableModel();
		m.setEditable(false);
		LCTableModel m2 = (LCTableModel) totalPosPanel.getMatrixTableModel();
		m2.setEditable(false);
		LCTableModel m3 = (LCTableModel) totalNegPanel.getMatrixTableModel();
		m3.setEditable(false);
	}

	public Project getProject() {
		return project;
	}

	public void setProject(Project modelProject, DataType dataType,
			Object attributes) {
		zonePanel.setProject(modelProject, dataType, attributes);
		LCTableModel m = (LCTableModel) netPanel.getMatrixTableModel();
		m.setProject(modelProject, dataType, ValueType.TOTAL, attributes);
		LCTableModel m2 = (LCTableModel) totalPosPanel.getMatrixTableModel();
		m2.setProject(modelProject, dataType, ValueType.TOTAL_POSITIVE,
				attributes);
		LCTableModel m3 = (LCTableModel) totalNegPanel.getMatrixTableModel();
		m3.setProject(modelProject, dataType, ValueType.TOTAL_NEGATIVE,
				attributes);
	}

	public void setLabel(String[] tabLabel) {
		for (int i = 0; i < tabLabel.length; i++) {
			if(i < tabPanel.getTabCount())
				tabPanel.setTitleAt(i, tabLabel[i]);
		}
	}

	public void setCornerSetting(String unitDef, String unitArea,
			boolean isUnitSelectable) {
		zonePanel.setCornerSetting(unitDef, unitArea, isUnitSelectable);
		netPanel.setCornerSetting(unitDef, unitArea, isUnitSelectable);
		totalNegPanel.setCornerSetting(unitDef, unitArea, isUnitSelectable);
		totalPosPanel.setCornerSetting(unitDef, unitArea, isUnitSelectable);
	}

	public void setZoneAreaScale(boolean isZoneAreaScale) {
		zonePanel.setZoneAreaScale(isZoneAreaScale);
		netPanel.setZoneAreaScale(isZoneAreaScale);
		totalNegPanel.setZoneAreaScale(isZoneAreaScale);
		totalPosPanel.setZoneAreaScale(isZoneAreaScale);
	}
	
	public void setSelectUnitArea(boolean b) {
		zonePanel.setSelectUnitArea(b);
		netPanel.setSelectUnitArea(b);
		totalNegPanel.setSelectUnitArea(b);
		totalPosPanel.setSelectUnitArea(b);
	}

	public LCMatrixTabPanel getZonePanel() {
		return zonePanel;
	}
	
	public void addZoneTabListener(ChangeListener l) {
		zonePanel.addZoneTabListener(l);
	}
	
	public void selectZoneTab(int i) {
		zonePanel.selectZoneTab(i);
	}

}